package projectManagementServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class AddNewProjectGUI extends JPanel implements ActionListener{
	JFrame f;
	private JTextField textProject;

	public AddNewProjectGUI() {
		setPreferredSize(new Dimension(364, 125));
		setLayout(null);
		
		JLabel lblProjectName = new JLabel("Project Name");
		lblProjectName.setHorizontalAlignment(SwingConstants.CENTER);
		lblProjectName.setBounds(136, 31, 90, 14);
		add(lblProjectName);
		
		JButton btnOk = new JButton("OK");
		btnOk.setBounds(82, 79, 101, 40);
		btnOk.putClientProperty("Quaqua.Button.style", "bevel");
		btnOk.addActionListener(this);
		add(btnOk);
		
		textProject = new JTextField();
		textProject.setBounds(83, 45, 198, 34);
		add(textProject);
		textProject.setColumns(10);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(181, 79, 101, 40);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		btnCancel.addActionListener(this);
		add(btnCancel);
		
		f = new JFrame();
		f.getContentPane().add(this);
		f.setVisible(true);
		f.setResizable(false);
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.pack();
		f.setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		
		if(cmd.equalsIgnoreCase("OK")){
			Project proj = new Project(textProject.getText(), "", (dateFormat.format(cal.getTime()))+"","ONGOING");
			AccessDatabase dataBase = new AccessDatabase();
			
			try {
				dataBase.addProject(proj);
			} catch (Exception e) {e.printStackTrace();}
			
			ServerGUI.addProject(proj);
			f.setVisible(false);
		}
		
		if(cmd.equalsIgnoreCase("CANCEL")){
			f.setVisible(false);
		}
		
	}
}
